/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatelparser.tags;

import alcatelparser.modules.AlgoUtil;
import alcatelparser.modules.BuilderFactory;
import alcatelparser.modules.I;

/**
 *
 * @author geniuz
 */
public class ChargingInformationTag extends DefaultParserTag
{

    public ChargingInformationTag()
    {
        _isAdditionalInType = false;
    }

    public final void parseTag(int ai[], int i)
    {
        _array = ai;
        _startParsePosition = i;
        setStandartLength(ai, i);/*
        if(isFullForm())
            BuilderFactory.getBuilder().writeTagContent(3 + _shift, 0, getTagName(), getTagIdentifyContent(i, i + 1));
        parseChargingInformationType(i + 2);*/
    }

    private String getTagName()
    {
        return _tagIdentifyValue != null ? I.I(6417) : getTagPropertiesValue(I.I(6392));
    }

    private void parseChargingInformationType(int i)
    {
        int j = _array[i];
        int k = calculateInnerTagType(j);
        if(isFullForm())
            BuilderFactory.getBuilder().writeTagContent(4 + _shift, 0, getTagPropertiesValue((new StringBuilder()).append(I.I(6372)).append(k).toString()), getTagIdentifyContent(i, i + 1));
        switch(k)
        {
        case 1: // '\001'
            parseRecordedUnits(i);
            break;

        case 2: // '\002'
            parseFreeOfCharge();
            break;

        case 3: // '\003'
            parseNotAvailableUnitList();
            break;
        }
    }

    private void parseNotAvailableUnitList()
    {
        if(isFullForm())
            BuilderFactory.getBuilder().writeTagContent(7 + _shift, 0, getTagPropertiesValue(I.I(6240)), "");
        else
            BuilderFactory.getBuilder().writeTagContent(7 + _shift, 0, getReducedTagPropertiesValue(I.I(6240)), "");
    }

    private void parseFreeOfCharge()
    {
        if(isFullForm())
            BuilderFactory.getBuilder().writeTagContent(7 + _shift, 0, getTagPropertiesValue(I.I(6332)), "");
        else
            BuilderFactory.getBuilder().writeTagContent(7 + _shift, 0, getReducedTagPropertiesValue(I.I(6332)), "");
    }

    private void parseRecordedUnits(int i)
    {
        int j = AlgoUtil.getLength(_array, i + 1);
        int k = 2;
        do
        {
            if(k >= j)
                break;
            int l = _array[i + 2];
            int i1 = _array[i + 3];
            if(isFullForm())
            {
                BuilderFactory.getBuilder().writeTagContent(5 + _shift, 0, getTagPropertiesValue(I.I(6279)), getTagIdentifyContent(i + 2, i + 3));
                BuilderFactory.getBuilder().writeTagContent(6 + _shift, 0, getTagPropertiesValue(I.I(2353)), "");
            } else
            if(isTagChecked(I.I(2339)))
            {
                String s = isFieldsDecoding() ? I.I(6319) : I.I(1023);
                BuilderFactory.getBuilder().writeTagContent(1 + _shift, 0, getChargingUnitsContentType(), s);
            }
            int j1 = i + 4;
            if(toHex(l).equals(I.I(6329)))
                k += parseRecordedNumberOfUnits(j1);
            else
                k += parseNotAvailable(j1);
            if(i1 - 3 > 0)
            {
                int k1 = _array[i1 + 1];
                if(isFullForm())
                    BuilderFactory.getBuilder().writeTagContent(7 + _shift, 2, getTagPropertiesValue(I.I(2476)), toHex(k1));
                else
                if(isTagChecked(I.I(2384)))
                    BuilderFactory.getBuilder().writeTagContent(1 + _shift, 2, getTypeOfChargingUnitsTagContent(), (new StringBuilder()).append("").append(k1).toString());
            }
        } while(true);
    }

    private String getChargingUnitsContentType()
    {
        if(_isAdditionalInType)
            return getReducedTagPropertiesValue(I.I(3546));
        else
            return getReducedTagPropertiesValue(I.I(2353));
    }

    private String getTypeOfChargingUnitsTagContent()
    {
        if(_isAdditionalInType)
            return getReducedTagPropertiesValue(I.I(3614));
        else
            return getReducedTagPropertiesValue(I.I(2476));
    }

    private int parseNotAvailable(int i)
    {
        BuilderFactory.getBuilder().writeTagContent(7 + _shift, 0, getTagPropertiesValue(I.I(6240)), "");
        return AlgoUtil.getLength(_array, i + 1);
    }

    private int parseRecordedNumberOfUnits(int i)
    {
        if(isFullForm())
        {
            String s = (new StringBuilder()).append(getTagIdentifyContent(i, i + 1)).append(toHex(_array[i + 2])).toString();
            BuilderFactory.getBuilder().writeTagContent(7 + _shift, 0, getTagPropertiesValue(I.I(2406)), s);
        } else
        if(isTagChecked(I.I(2384)))
            BuilderFactory.getBuilder().writeTagContent(7 + _shift, 0, getNumberOfChargingUnitsTagContent(), (new StringBuilder()).append("").append(_array[i + 2]).toString());
        return AlgoUtil.getLength(_array, i + 1) + 2;
    }

    private String getNumberOfChargingUnitsTagContent()
    {
        if(_isAdditionalInType)
            return getReducedTagPropertiesValue(I.I(6208));
        else
            return getReducedTagPropertiesValue(I.I(2406));
    }

    public final String getParseIdentityTag()
    {
        return I.I(6205);
    }

    public final void setTagIdentifyContent(String s)
    {
        _tagIdentifyValue = s;
    }

    public final void setShift(int i)
    {
        _shift = i;
    }

    public final void setIsAdditionalType(boolean flag)
    {
        _isAdditionalInType = flag;
    }

    private boolean _isAdditionalInType;
    private int _shift;
    private String _tagIdentifyValue;
}
